package com.kzd.btgame.main.mine

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.AppUtils
import com.kzd.btgame.R
import com.kzd.btgame.app.AppFlowBus
import com.kzd.btgame.app.GTApplication
import com.kzd.btgame.databinding.ActivityMineDownloadBinding
import com.kzd.btgame.db.Download
import com.kzd.btgame.manage.DownloadManager
import com.kzd.btgame.main.game.adapter.GameDownloadAdapter
import com.kzd.btgame.main.popup.DownloadDeletePopup
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.owner.OwnerViewBindingActivity

class MineDownloadActivity : OwnerViewBindingActivity<ActivityMineDownloadBinding>() {

    private val mGameDownloadAdapter = GameDownloadAdapter()

    private val mDownloadManager by lazy {
        DownloadManager.instance
    }

    private val mDownloadDeletePopup: DownloadDeletePopup by lazy {
        DownloadDeletePopup(context = this) { _, download ->
            mDownloadManager.deleteFile(download.localPath)
            mDownloadManager.deleteDownload(download.gameId)
        }
    }

    override val inflate: (LayoutInflater) -> ActivityMineDownloadBinding
        get() = ActivityMineDownloadBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flGameDownloadBar
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {


        mViewBinding.rvGameDownloadBody.apply {
            itemAnimator?.addDuration = 0
            itemAnimator?.changeDuration = 0
            itemAnimator?.moveDuration = 0
            itemAnimator?.removeDuration = 0
        }.adapter = mGameDownloadAdapter.apply {
            setNewInstance(GTApplication.appDatabase.gameDownloadDao().loadAll())
        }

        mGameDownloadAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mGameDownloadAdapter.getItem(position)
            if (view.id == R.id.tvGameDownloadAction) {
                if (itemData.downStatus == 1 || itemData.downStatus == 3) {
                    mDownloadManager.download(
                        gameId = itemData.gameId,
                        gameName = itemData.gameName,
                        gameIcon = itemData.gameIcon,
                        downPath = itemData.downPath
                    )
                } else if (itemData.downStatus == 2) {
                    AppUtils.installApp(itemData.localPath)
                } else if (itemData.downStatus == 4) {
                    val packageName = AppUtils.getApkInfo(itemData.localPath)
                        ?.packageName ?: return@setOnItemChildClickListener
                    AppUtils.launchApp(packageName)
                } else {
                    mDownloadManager.stopDownload(itemData.gameId)
                }
            } else if (view.id == R.id.tvGameDownloadDelete) {
                mDownloadDeletePopup.setData(itemData).showPopupWindow()
            }
        }
    }

    override fun initViewMode() {

        AppFlowBus.observe(this as LifecycleOwner, Download::class.java, Observer<Download> { down ->
            val index = mGameDownloadAdapter.data
                .indexOfLast { it.gameId == down.gameDownload?.gameId }
            if (index == -1 || down.gameDownload == null)
                return@Observer
            if (down.action == Download.Action.ACTION_UPDATE) {
                mGameDownloadAdapter.setData(index, down.gameDownload)
            } else if (down.action == Download.Action.ACTION_DELETE) {
                mGameDownloadAdapter.removeAt(index)
            }
        })
    }

    override fun onResume() {
        super.onResume()
        mDownloadManager.initData()
    }

}